﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;

public partial class Account_Login : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }
  
    protected void btnLogin_Click(object sender, ImageClickEventArgs e)
    {
        //Response.Write("ccc");
        string userName = tbUserName.Text.Trim();
        string password = tbPassword.Text.Trim();
        string vcode = tbVcode.Text.Trim().ToLower();
        #region 服务端验证
        errors.Text = "";
        if (userName.Length == 0)
            errors.Text += "输入用户名.\\n";
        if (password.Length == 0)
            errors.Text += "输入登陆密码.\\n";
        if (vcode.Length == 0)
            errors.Text += "填写验证码.\\n";
        if (Session["vcode"] != null && Session["vcode"].ToString().ToLower() != vcode)
            errors.Text += "验证码输入有误.\\n";

        if (errors.Text != "")
        {
            tbVcode.Text = "";
            errors.Text = "<script>alert('"+errors.Text+"');</script>";
            return;
        }
        #endregion
        password = System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(password, "MD5");
        MatchIns.Model.TMemberLogin member = new MatchIns.BLL.TMemberLogin().Login(userName, password);
        if (member == null)
        {
            errors.Text += "用户名密码有误.\\n";
            errors.Text = "<script>alert('" + errors.Text + "');</script>";
            tbVcode.Text = "";
        }
        else
        {
            Session["user"] = member;
            string from = Request.QueryString["From"];
            if (member.UserTypeId == (int)MatchIns.UserType.代理人)
            {
                if (from == null)
                    Response.Redirect("~/Agent", true);
                else
                    Response.Redirect(from, true);
            }
            else
            {
                if (from == null)
                    Response.Redirect("~/PolicyHolder", true);
                else
                    Response.Redirect(from, true);
            }
        }
    }
}
